package com.demo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @BelongsProject: study
 * @BelongsPackage: com.demo
 * @Author: zhangzhanheng
 * @CreateTime: 2024-01-22  09:56
 * @Description: TODO
 * @Version: 1.0
 */
public class Demo {
    public static void main(String[] args) {
        List<MyObject> myList = Arrays.asList(
                new MyObject("A", new Date(2023,11,22,10,10,10)),
                new MyObject("B", new Date(2023,11,22,10,10,11)),
                new MyObject("A", new Date(2023,11,22,10,10,12)),
                new MyObject("B", new Date(2023,11,22,10,10,13)),
                new MyObject("A", new Date(2023, Calendar.DECEMBER,22,10,10,14))
        );

        // 先按 category 分组，然后对每个组内的元素按 value 排序
        Map<String, List<MyObject>> groupedAndSorted = myList.stream()
                .collect(Collectors.groupingBy(MyObject::getCategory,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> list.stream()
                                        .sorted((obj1, obj2) -> obj1.getValue().compareTo(obj2.getValue()))
                                        .collect(Collectors.toList())
                        )
                ));

        // 打印结果
        groupedAndSorted.forEach((category, objects) -> {
            System.out.println("Category: " + category);
            objects.forEach(obj -> System.out.println("  Value: " + obj.getValue()));
        });
    }
}
